<?php
/**
 * Zend Framework
 *
 * LICENSE
 *
 * This source file is subject to the new BSD license that is bundled
 * with this package in the file LICENSE.txt.
 * It is also available through the world-wide-web at this URL:
 * http://framework.zend.com/license/new-bsd
 * If you did not receive a copy of the license and are unable to
 * obtain it through the world-wide-web, please send an email
 * to license@zend.com so we can send you a copy immediately.
 *
 * @package    Zend_Controller
 * @subpackage Router
 * @copyright  Copyright (c) 2005-2008 Zend Technologies USA Inc. (http://www.zend.com)
 * @version    $Id: Rewrite.php 9414 2008-05-08 15:48:05Z martel $
 * @license    http://framework.zend.com/license/new-bsd     New BSD License
 */

/**
 * Ruby routing based Router.
 *
 * @package    Zend_Controller
 * @subpackage Router
 * @copyright  Copyright (c) 2005-2008 Zend Technologies USA Inc. (http://www.zend.com)
 * @license    http://framework.zend.com/license/new-bsd     New BSD License
 * @see        http://manuals.rubyonrails.com/read/chapter/65
 */
class Framework_Controller_Router_Rewrite
{
    /**
     * @const string URI delimiter
     */
    const URI_DELIMITER = '/';
    
    protected $_controllerKey = 'controller';
    protected $_actionKey     = 'action';
    
    /**
     * Default values for the route (ie. module, controller, action, params)
     * @var array
     */
    protected $_defaultController = 'index';
    protected $_defaultAction     = 'index';
    
    protected $_defaults = array();
    
    
    protected function _setDefaultKeys()
    {
        $this->_defaults = array(
                $this->_controllerKey => $this->_defaultController,
                $this->_actionKey     => $this->_defaultAction
            );
    }
    /**
     * Matches a user submitted path. Assigns and returns an array of variables
     * on a successful match.
     *
     * If a request object is registered, it uses its setModuleName(),
     * setControllerName(), and setActionName() accessors to set those values.
     * Always returns the values as an array.
     *
     * @param string $path Path used to match against this routing map
     * @return array An array of assigned values or a false on a mismatch
     */
    protected function _match($path)
    {
        $this->_setDefaultKeys();
        
        $values = array();
        $params = array();
        $path   = trim($path, self::URI_DELIMITER);

        if ($path != '') {

            $path = explode(self::URI_DELIMITER, $path);

            // admin зарезервирован для админки
            if (!empty($path[0]) && $path[0] == 'admin') {
                $values['admin'] = array_shift($path);
            }
            
            if (count($path) && !empty($path[0])) {
                $values[$this->_controllerKey] = array_shift($path);
            }

            if (count($path) && !empty($path[0])) {
                $values[$this->_actionKey] = array_shift($path);
            }

            if ($numSegs = count($path)) {
                for ($i = 0; $i < $numSegs; $i++) {
                    $val = urldecode($path[$i]);
                    $params[] = $val;
                }
            }
        }

        $this->_values = $values + $params;

        return $this->_values + $this->_defaults;
    }
    
    /**
     * Find a matching route to the current PATH_INFO and inject
     * returning values to the Request object.
     *
     * @throws Zend_Controller_Router_Exception
     * @return Zend_Controller_Request_Abstract Request object
     */
    public function route(Framework_Controller_Request_Abstract $request)
    {
        if (!$request instanceof Framework_Controller_Request_Http) {
            require_once 'Framework/Controller/Router/Exception.php';
            throw new Framework_Controller_Router_Exception('Framework_Controller_Router_Rewrite requires a Zend_Controller_Request_Http-based request object');
        }

        $pathInfo = $request->getPathInfo();

        /** Find the matching route */
        if ($params = $this->_match($pathInfo)) {
            $this->_setRequestParams($request, $params);
        }

        return $request;

    }

    protected function _setRequestParams($request, $params)
    {
        foreach ($params as $param => $value) {

            $request->setParam($param, $value);

            if ($param === $request->getControllerKey()) {
                $request->setControllerName($value);
            }
            if ($param === $request->getActionKey()) {
                $request->setActionName($value);
            }

        }
    }

}
